package com.ef.web.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ELeadsDealType;
import com.ef.domain.Leads;
import com.ef.domain.LeadsDealHistory;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.util.CustomExample;
import com.ef.util.LeadsExample;

public class CRAssignAction extends LeadsAction {
	private static final long serialVersionUID = 1L;

	private Map<String, String> crs;

	private boolean noCr = true;

	@SuppressWarnings("serial")
	public String list() {
		fetchListData();
		CustomExample<Leads> example = new LeadsExample(this.getModel(),
				Leads.class) {
			public void appendCondition(Criteria criteria) {
				super.appendCondition(criteria);
				criteria.add(Restrictions.isNotNull("eliteCard"));
				if (entity.getCr() == null || entity.getCr().getName() == null
						|| entity.getCr().getName().length() == 0) {
					if (noCr) {
						criteria.add(Restrictions.isNull("cr"));
					}
				}
				criteria.addOrder(Order.desc("createTime"));
			}
			
		};
		example.addBetweenDateProperty("createTime", selCreateStartTime,
				selCreateEndTime);
		example.addBetweenDateProperty("setApptTime", selSetApptStartTime,
				selSetApptEndTime);
		example.addBetweenDateProperty("showTime", selShowStartTime,
				selShowEndTime);
		example.addBetweenDateProperty("lastEventTime", selLastTouchStartTime,
				selLastTouchEndTime);
		example.addBetweenDateProperty("payTime", selPayStartTime,
				selPayEndTime);
		example.addBetweenDateProperty("crAssignTime", selAssignTmStartTime,
				selAssignTmEndTime);
		example.addBetweenDateProperty("originalClassTime",
				selOriginalClassTimeStartTime, selOriginalClassTimeEndTime);
		this.listResult = leadsService.findByExample(example, this
				.getStartIndex(), this.getPageSize());
		return LIST;
	};

	public String assignToCr() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		String tm = getRequest().getParameter("cr_id");
		User user = userService.findById(Long.valueOf(tm));
		for (String i : id) {
			Leads l = leadsService.findById(Long.valueOf(i));
			LeadsDealHistory dh=new LeadsDealHistory();
			dh.setCurState(user.getName());
			if (l.getCr()!=null){
				dh.setPreState(l.getCr().getName());
			}
			dh.setDealTime(new Date());
			dh.setOperUser(this.getSessionUser());
			dh.setType(ELeadsDealType.AssignedToCR);
			dh.setLeads(l);
			leadsDealHistoryService.saveOrUpdate(dh);
			l.setCr(user);
			l.setCrAssignTime(new Date());
			leadsService.saveOrUpdate(l);
			
		}
		fetchListData();
		return OK;
	}

	protected void fetchListData() {
		super.fetchListData();
		Map<String, String> map = new LinkedHashMap<String, String>();
		School school = getSessionUser().getSchool();
		List<User> users = null;
		if(school == null)
		 users = userService.findAllCr();
		else {
			users = userService.findAllCrSameSchool(school);
		}
		for (User u : users) {
			map.put(u.getId().toString(), u.getName());
		}
		crs = map;
	}

	public Map<String, String> getCrs() {
		return crs;
	}

	public void setCrs(Map<String, String> crs) {
		this.crs = crs;
	}

	public boolean isNoCr() {
		return noCr;
	}

	public void setNoCr(boolean noCr) {
		this.noCr = noCr;
	}

}
